package com.xxcloud.model.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author yu
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class ListResult<T> implements Serializable {
    /**
     */
    private static final long serialVersionUID = 1L;

    private List<T> result = new ArrayList<T>();

    private int total;

    private List<PageLink> pages;

    private Map<String, Object> extraDataMap;

    private String param;

    private int pageTotal;

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void createPages(int currentPageNo, int pageSize) {
        int totalPages = (int) Math.ceil((double) total / pageSize);
        pageTotal = totalPages;
        System.out.println("total pages " + totalPages);

        List<PageLink> links = new ArrayList<PageLink>();

        int minPage = currentPageNo - 3, maxPage = currentPageNo + 3;

        if (minPage < 1) {
            minPage = 1;
        }
        minPage = Math.min(minPage, currentPageNo);
        System.out.println("minPage " + minPage);

        maxPage = Math.min(maxPage, totalPages);
        System.out.println("maxPage " + maxPage);

        if (minPage > 1) {// 当前分页最小页数大于第一页
            links.add(new PageLink(1, "首页", true));
        }

        for (int pageNum = minPage; pageNum <= maxPage; pageNum++) {
            if (pageNum == currentPageNo) {
                links.add(new PageLink(pageNum, String.valueOf(pageNum), false));
            } else {
                links.add(new PageLink(pageNum, String.valueOf(pageNum), true));
            }
        }

        if (maxPage < totalPages) {// 当前分页最大页数小于最后一页
            links.add(new PageLink(totalPages, "尾页", true));
        }

        setPages(links);
    }

    public List<PageLink> getPages() {
        return pages;
    }

    public void setPages(List<PageLink> pages) {
        this.pages = pages;
    }

    public Map<String, Object> getExtraDataMap() {
        return extraDataMap;
    }

    public void setExtraDataMap(Map<String, Object> extraDataMap) {
        this.extraDataMap = extraDataMap;
    }

    public void putExtraData(String key, Object value) {
        if (key == null || value == null) {
            return;
        }
        if (extraDataMap == null || extraDataMap.size() <= 0) {
            extraDataMap = new HashMap<>();
        }
        extraDataMap.put(key, value);
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }
}
